<template>
  <div>
    <Modal
      :value="modalState"
      :title="title"
      :loading="loading"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="600">
      <Form
        ref="statisticalModal"
        :model="row"
        :rules="ruleInline"
        inline
        style="width: 100%"
        :label-width="85">
        <FormItem v-show="!row.id" prop="proid" label="项目id" style="width: 250px">
          <Select v-model="row.proid" clearable placeholder="请选择项目ID">
            <Option v-for="item in project" :value="item.id" :key="item.id">
              {{ item.name }}
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="ctime" label="创建时间" style="width: 250px">
           <DatePicker v-model="row.ctime" type="date" placeholder="请输入创建时间"></DatePicker>
          <!-- <TimePicker type="time" placeholder="请选择时间"></TimePicker> -->
        </FormItem>
        <FormItem prop="cpa" label="注册数" style="width: 250px">
          <Input type="text" v-model="row.cpa" placeholder="请输入注册数" />
        </FormItem>
        <FormItem prop="rz" label="认证数" style="width: 250px">
          <Input type="text" v-model="row.rz" placeholder="请输入认证数" />
        </FormItem>
        <FormItem prop="uv" label="uv数量" style="width: 250px">
          <Input type="text" v-model="row.uv" placeholder="请输入uv数量" />
        </FormItem>
        <FormItem prop="yreturn" label="应回款数量" style="width: 250px">
          <Input type="text" v-model="row.yreturn" placeholder="请输入应回款数量" />
        </FormItem>
        <FormItem prop="return" label="实际回款数量" style="width: 250px">
          <Input type="text" v-model="row.return" placeholder="请输入实际回款数量" />
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
export default {
  name: 'StatisticalModal',
  data () {
    return {
      title: '',
      loading: true,
      form: {
        proid: '',
        cpa: '',
        rz: '',
        uv: '',
        ctime: '',
        yretutn: '',
        return: ''
      },
      ruleInline: {
        proid: [
          { type: 'number', required: true, message: '项目id不能为空', trigger: 'change' }
        ]
      }
    }
  },
  updated () {
    if (this.row.id && this.row.id !== '') {
      this.title = '修改客服业务信息'
    } else {
      this.title = '添加客服业务记录'
    }
  },
  computed: {
    ...mapState({
      row: state => state.statistical.row,
      modalState: state => state.modal.modalState,
      project: state => state.operate.project
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.statisticalModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('statistical-valid', {
          id: this.row.id,
          proid: this.row.proid ? parseInt(this.row.proid) : '',
          cpa: this.row.cpa !== '' ? parseInt(this.row.cpa) : '',
          rz: this.row.rz !== '' ? parseInt(this.row.rz) : '',
          uv: this.row.uv !== '' ? parseInt(this.row.uv) : '',
          ctime: this.row.ctime ? this.$moment(this.row.ctime).format('YYYY-MM-DD') : this.$moment(new Date()).format('YYYY-MM-DD'),
          yreturn: this.row.yreturn !== '' ? parseInt(this.row.yreturn) : '',
          return: this.row.return !== '' ? parseInt(this.row.return) : ''
        })
        this.$refs['statisticalModal'].resetFields()
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['statisticalModal'].resetFields()
    }
  }
}
</script>
